package aos.dijkstra;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import aos.exception.AosException;

public class GlobalVariables {
	public static int port = 9988;
	public static int loglevel = 0xffffffff;
	public static String logfile = null;
	public static String iptable = "iptable.txt";
	public static String events = "events.txt";

	public static void initialize(String configsFile) throws AosException {
		Properties properties = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(configsFile);
		} catch (FileNotFoundException e) {
			System.out.println("file \"" + configsFile + "\" not found");
			e.printStackTrace();
			throw new AosException(e.getMessage(), e);
		}
		System.out.println("configsFile open success");
		try {
			properties.load(fis);
		} catch (IOException e) {
			System.out.println("configsFile \"" + configsFile
					+ "\" load failed");
			e.printStackTrace();
			throw new AosException(e.getMessage(), e);
		}
		System.out.println("configsFile load success");

		if (properties.getProperty("port") != null) {
			port = Integer.parseInt(properties.getProperty("port"));
			System.out.println("port for listening is: " + port);
		} else {
			System.out.println("port for listening is: " + port + " (default)");
		}

		if (properties.getProperty("loglevel") != null) {
			loglevel = Integer.parseInt(properties.getProperty("loglevel"));
			System.out.println("loglevel is: " + Integer.toHexString(loglevel));
		} else {
			System.out.println("loglevel is: " + Integer.toHexString(loglevel)
					+ " (default)");
		}

		if (properties.getProperty("logfile") != null) {
			logfile = properties.getProperty("logfile");
			System.out.println("logfile is: " + logfile);
		} else {
			System.out.println("logfile is: " + logfile + " (default)");
		}

		if (properties.getProperty("iptable") != null) {
			iptable = properties.getProperty("iptable");
			System.out.println("iptable is: " + iptable);
		} else {
			System.out.println("iptable is: " + iptable + " (default)");
		}

		if (properties.getProperty("events") != null) {
			events = properties.getProperty("events");
			System.out.println("events is: " + events);
		} else {
			System.out.println("events is: " + events + " (default)");
		}
	}
}
